import request from '@/utils/request'
import {handleRvaButtonClick} from '@/api/rva/crud'
import {RvaUtils} from '@/api/rva/util'

export default {
  props: ['viewButtonData', 'selection', 'disabled', 'formData', 'formRef', 'requestParams'],
  components: {
    'rva-form': () => import('@/components/rva/form'),
    'rva-crud': () => import('@/components/rva/crud'),
    'rva-tcrud': () => import('@/components/rva/tcrud')
  },
  data() {
    return {
      // 遮罩层
      loading: false,
      open: false,
      dialogFormParams: false,
      dialogAppParams: {trigger: 0},
      dialogWidth: '80%',
      show: false
    };
  },
  created() {
    // 按钮点击弹框视图数据
    this.dialogViewData = false
    this.ifShow ();
  },
  methods: {
    setDialogWidth (width) {
      this.dialogWidth = width + 'px';
    },
    /** 处理按钮点击事件 */
    handleButtonClick() {
      console.log ('handleButtonClick....', this.viewButtonData);
      // 显示按钮遮罩层，不允许再次点击
      this.loading = true;
      if (this.viewButtonData.type == 'js') {
        if (this.viewButtonData.action == 'reload') {
          location.reload();
        } else {
          this.$emit('rva-button-click', [{action: this.viewButtonData.action}]);
          // 隐藏按钮遮罩层，允许再次点击
          this.loading = false;
        }
        return;
      }
      // 增加参数
      var params = {};
      if (this.selection) {
        params.selection = this.selection
      }
      RvaUtils.clone(this.requestParams, params);
      if (this.viewButtonData.type == 'tcrud') {
        if (this.viewButtonData.action == 'tab') {
          this.$router.push ({path: this.viewButtonData.actionUrl});
          sessionStorage.setItem(this.viewButtonData.actionUrl, JSON.stringify(params));
          return;
        }
        if (this.viewButtonData.action == 'dialog') {
          // 显示对话框
          this.open = true;
          this.dialogAppParams.rvaAppParams = params;
          this.dialogAppParams.trigger ++;
          // 隐藏按钮遮罩层，允许再次点击
          this.loading = false;
          return;
        }
      }
      if (this.viewButtonData.type == 'form') {
        // 显示对话框
        console.log ('button.request...');
        this.dialogFormParams = params;
        this.open = true;
        // 隐藏按钮遮罩层，允许再次点击
        this.loading = false;
        return;
      }
      if (this.viewButtonData.type == 'crud') {
        if (this.viewButtonData.action == 'tab') {
          this.$router.push ({path: this.viewButtonData.actionUrl});
          sessionStorage.setItem(this.viewButtonData.actionUrl, JSON.stringify(params));
          return;
        }
        if (this.viewButtonData.action == 'dialog') {
          // 显示对话框
          this.open = true;
          this.dialogAppParams.rvaAppParams = params;
          this.dialogAppParams.trigger ++;
          // 隐藏按钮遮罩层，允许再次点击
          this.loading = false;
          return;
        }
      }
      if (this.viewButtonData.type == 'ajax') {
        console.log ('ajax', this.formData, params, this.formRef, this.$refs);
        if (RvaUtils.parseValue(this.viewButtonData.data, 'validateForm') == 'N' || !this.formRef) {// 不校验表单
          this.ajax(params);
          return;
        }
        this.formRef.validate((valid, object) => {
          if (valid) {
            this.ajax(params);
          } else {
            console.log('validateForm error!!', object);
            // 隐藏按钮遮罩层，允许再次点击
            this.loading = false;
            return false;
          }
        });
        return;
      }
    },
    ajax(params) {
      RvaUtils.clone(this.formData, params);
      request({
        url: this.viewButtonData.actionUrl,
        method: 'post',
        data: params
      }).then(response => {
        this.msgSuccess("操作成功！");
        var actions = [];
        var closeView = true;
        if (this.viewButtonData.actionSuccess) {
          this.viewButtonData.actionSuccess.split(',').forEach(function (action) {
            if (action == 'keepView') {
              closeView = false;
            } else {
              actions.push({
                action
              });
            }
          });
        }
        if (closeView) {
          actions.push({
            action: 'closeView'
          });
        }
        this.$emit('rva-button-click', actions);
        // 隐藏按钮遮罩层，允许再次点击
        this.loading = false;
      }).catch(err => {
        console.log (this.viewButtonData.actionUrl, err);
        // 隐藏按钮遮罩层，允许再次点击
        this.loading = false;
      });
    },
    closeView (action) {// handleRvaButtonClick中会调用
      this.open = false;
    },
    handleFormButtonClick (data) {
      handleRvaButtonClick(this, data);
    },
    handleButtonConfigClick (data) {
      handleRvaButtonClick(this, data);
    },
    ifShow () {
      // console.log ('this.viewButtonData.showIf', this.viewButtonData.showIf);
      if (this.viewButtonData.position == 'inner') {
        this.show = true;
      } else {
        if (RvaUtils.isEmpty(this.viewButtonData.showIf)) {
          this.show = true;
        } else {
          this.show = eval ('(' + this.viewButtonData.showIf + ')');
        }
      }
      // console.log ('this.show', this.viewButtonData.id, this.show);
    },
    getConfirmMessage () {
      let confirmMessage = RvaUtils.parseValue(this.viewButtonData.data, 'confirmMessage');
      if (this.viewButtonData.cls == 'danger') {
        confirmMessage = '该操作比较危险，请再次确认！';
      }
      return confirmMessage;
    }
  },
  watch: {
    requestParams: {
      handler() {
        this.ifShow();
      },
      deep: true
    },
    selection: {
      handler() {
        this.ifShow();
      },
      deep: true
    }
  }
}
